﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;

namespace Admin
{
    public partial class MainForm
    {
        private void btnSearchCourse_Click(object sender, EventArgs e)
        {
            using (var context = new ModelContainer())
            {
                var result = context.Courses.Include("Teacher").Include("CourseInfo").Include("CoursePlaces").AsEnumerable();
                var valueAll = txtSearchCoursesKeywords.Text.Split(' ');
                result = valueAll.Aggregate(result, (current, v) => current.Where(course => course.CourseName.Contains(v) || course.Teacher.TeacherName.Contains(v) || course.Type.Contains(v) || course.Department.Contains(v) || course.CourseId.Contains(v)));
                gridCourses.AutoGenerateColumns = false;
                gridCourses.DataSource = result.ToList();
            }
        }

        private void gridCourses_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            var id = (int) gridCourses.CurrentRow.Cells["ID"].Value;
            using (var context = new ModelContainer())
            {
                var course =
                    context.Courses.Single(c => c.ID == id);
                IsExisting.Checked = course.IsExisting;
                var type = course.GetType();
                foreach (var property in type.GetProperties())
                {
                    var cons = Controls.Find("txt" + property.Name, true);
                    if (cons.Length > 0)
                    {
                        (cons[0] as TextBox).Text = (string)property.GetValue(course, null);
                    }
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            var id = (int)gridCourses.CurrentRow.Cells["ID"].Value;
            using (var context = new ModelContainer())
            {
                var course =
                    context.Courses.Single(c => c.ID == id);
                course.IsExisting = IsExisting.Checked;
                var type = course.GetType();
                foreach (var property in type.GetProperties())
                {
                    var cons = Controls.Find("txt" + property.Name, true);
                    if (cons.Length > 0)
                    {
                        property.SetValue(course, (cons[0] as TextBox).Text, null);
                    }
                }
                course.MarkAsModified();
                context.Courses.ApplyChanges(course);
                context.SaveChanges();
            }
            btnSearchCourse_Click(null, null);
        }

        private void btnDiscuss_Click(object sender, EventArgs e)
        {
            tabControl1.TabPages.Add(tabPage3);
            tabControl1.SelectTab(2);
            rdoHelp.Checked = true;
            rdoHelp_CheckedChanged(null, null);
        }

        private void rdoHelp_CheckedChanged(object sender, EventArgs e)
        {
            if (!rdoHelp.Checked)
                return;
            var id = (int)gridCourses.CurrentRow.Cells["ID"].Value;
            using (var context =  new ModelContainer())
            {
                var helps = context.Helps.Include("HelpReplys").Where(h => h.CourseID == id);
                Posts.DisplayMember = "Content";
                Posts.ValueMember = "ID";
                Posts.DataSource = helps.Select(help => new ListItem { ID = help.ID, Content = help.UserName + " - " + help.Content }).ToList();
            }
        }

        private void rdoChat_CheckedChanged(object sender, EventArgs e)
        {
            if (!rdoChat.Checked)
                return;
            var id = (int)gridCourses.CurrentRow.Cells["ID"].Value;
            using (var context = new ModelContainer())
            {
                var chats = context.Chats.Include("HelpReplys").Where(h => h.CourseID == id);
                Posts.DisplayMember = "Content";
                Posts.ValueMember = "ID";
                Posts.DataSource = chats.Select(chat => new ListItem { ID = chat.ID, Content = chat.UserName + " - " + chat.Content }).ToList();
            }
        }

        private void Posts_SelectedIndexChanged(object sender, EventArgs e)
        {
            var id = (int) Posts.SelectedValue;
            using (var context = new ModelContainer())
            {
                if (rdoHelp.Checked)
                {
                    var replys = context.HelpReplys.Where(h => h.HelpID == id);
                    Replys.DisplayMember = "Content";
                    Replys.ValueMember = "ID";
                    Replys.DataSource =
                        replys.Select(
                            reply => new ListItem {ID = reply.ID, Content = reply.UserName + " - " + reply.Content}).ToList();

                    var help = context.Helps.Single(r => r.ID == id);
                    txtReply.Text = help.Content;
                }
                else
                {
                    var replys = context.ChatReplys.Where(h => h.ChatID == id);
                    Replys.DisplayMember = "Content";
                    Replys.ValueMember = "ID";
                    Replys.DataSource =
                        replys.Select(
                            reply => new ListItem { ID = reply.ID, Content = reply.UserName + " - " + reply.Content }).ToList();

                    var help = context.Chats.Single(r => r.ID == id);
                    txtReply.Text = help.Content;
                }
            }
        }

        private void btnFindPost_Click(object sender, EventArgs e)
        {
            var id = (int)gridCourses.CurrentRow.Cells["ID"].Value;
            using (var context = new ModelContainer())
            {
                if (rdoHelp.Checked)
                {
                    var result = context.Helps.Where(help => help.CourseID == id);
                    result = txtPostSearch.Text.Split(' ')
                        .Aggregate(result,
                                   (current, keyword) =>
                                   current.Where(help =>
                                                 help.UserName.Contains(keyword)
                                                 || help.Content.Contains(keyword)
                                                 || help.HelpReplys.Count(reply =>
                                                                          reply.UserName.Contains(keyword)
                                                                          || reply.Content.Contains(keyword)) > 0));
                    Posts.DisplayMember = "Content";
                    Posts.ValueMember = "ID";
                    Posts.DataSource = result.Select(help => new ListItem { ID = help.ID, Content = help.UserName + " - " + help.Content }).ToList();
                    if (Posts.Items.Count == 0)
                        Replys.DataSource = null;
                }
                else
                {
                    var result = context.Chats.Where(chat => chat.CourseID == id);
                    result = txtPostSearch.Text.Split(' ')
                        .Aggregate(result,
                                   (current, keyword) =>
                                   current.Where(help =>
                                                 help.UserName.Contains(keyword)
                                                 || help.Content.Contains(keyword)
                                                 || help.ChatReplys.Count(reply =>
                                                                          reply.UserName.Contains(keyword)
                                                                          || reply.Content.Contains(keyword)) > 0));
                    Posts.DisplayMember = "Content";
                    Posts.ValueMember = "ID";
                    Posts.DataSource = result.Select(help => new ListItem { ID = help.ID, Content = help.UserName + " - " + help.Content }).ToList();
                    if (Posts.Items.Count == 0)
                        Replys.DataSource = null;
                }
            }
        }

        private void Replys_SelectedIndexChanged(object sender, EventArgs e)
        {
            var id = (int) Replys.SelectedValue;
            using (var context = new ModelContainer())
            {
                if (rdoHelp.Checked)
                {
                    var reply = context.HelpReplys.Single(r => r.ID == id);
                    txtReply.Text = reply.Content;
                }
                else
                {
                    var reply = context.ChatReplys.Single(r => r.ID == id);
                    txtReply.Text = reply.Content;
                }
            }
        }

        private void btnDelReply_Click(object sender, EventArgs e)
        {
            if (Replys.Items.Count == 0)
                return;
            var id = (int)Replys.SelectedValue;
            using (var context = new ModelContainer())
            {
                if (rdoHelp.Checked)
                {
                    var reply = context.HelpReplys.Single(r => r.ID == id);
                    context.DeleteObject(reply);
                    context.SaveChanges();
                    Posts_SelectedIndexChanged(null, null);
                }
                else
                {
                    var reply = context.ChatReplys.Single(r => r.ID == id);
                    context.DeleteObject(reply);
                    context.SaveChanges();
                    Posts_SelectedIndexChanged(null, null);
                }
            }
        }

        private void btnDelPost_Click(object sender, EventArgs e)
        {
            if (Posts.Items.Count == 0)
                return;
            var id = (int)Posts.SelectedValue;
            using (var context = new ModelContainer())
            {
                if (rdoHelp.Checked)
                {
                    var help = context.Helps.Include("HelpReplys").Single(r => r.ID == id);
                    var list = help.HelpReplys.ToList();
                    foreach (var reply in list)
                    {
                        context.DeleteObject(reply);
                    }
                    context.DeleteObject(help);
                    context.SaveChanges();
                    rdoHelp_CheckedChanged(null, null);
                }
                else
                {
                    var chat = context.Chats.Include("ChatReplys").Single(r => r.ID == id);
                    var list = chat.ChatReplys.ToList();
                    foreach (var reply in list)
                    {
                        context.DeleteObject(reply);
                    }
                    context.DeleteObject(chat);
                    context.SaveChanges();
                    rdoChat_CheckedChanged(null, null);
                }
            }
        }
    }

    public class ListItem
    {
        public int ID { get; set; }
        public string Content { get; set; }
    }
}
